//
//  Untitled.swift
//  PLDownload
//
//  Created by bin hu on 2025/7/7.
//

import Foundation

//enum DownloadError: Error {
//    case networkFailure(URLError)
//    case fileSystemError(Error)
//    case invalidProtocol
//    case insufficientDiskSpace
//    case decryptionFailed // 针对加密媒体
//}

enum DownloadError: Error, LocalizedError {
    case invalidKeyURI
    case keyDownloadFailed
    case invalidKeyLength
    case decryptionFailed
    case invalidSegment
    case invalidM3U8
    
    var errorDescription: String? {
        switch self {
        case .invalidKeyURI: return "Invalid key URI"
        case .keyDownloadFailed: return "Failed to download decryption key"
        case .invalidKeyLength: return "Invalid key length (must be 16 bytes)"
        case .decryptionFailed: return "Decryption failed"
        case .invalidSegment: return "Invalid media segment"
        case .invalidM3U8: return "Invalid M3U8 format"
        }
    }
}

// MARK: - 核心模型
enum DownloadState: Equatable {
    case queued
    case downloading(progress: Double)
    case paused
    case completed
    case failed(error: String)
    case decrypting
    case merging
}

//class DownloadTask {
//    var id: UUID
//    var url: URL
//    var state: DownloadState = .queued
//    var progress: Float = 0
//    var fileType: FileType
//    var tempPath: URL
//    var finalPath: URL
//    var protocolHandler: ProtocolHandler?
//}

struct DownloadTask: Identifiable {
    let id: UUID
    let url: URL
    let title: String
    var state: DownloadState = .queued
    var fileType: String
    var fileSize: Int64?
    var downloadedBytes: Int64 = 0
    var localPath: URL?
    var creationDate = Date()
    
    var progress: Double {
        switch state {
        case .downloading(let progress):
            return progress
        case .completed:
            return 1.0
        default:
            return 0
        }
    }
}
